/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.Reader;
import cn.ac.iie.di.File.FileTool;
import cn.ac.iie.di.base.Startup;
import cn.ac.iie.di.writer.Writer;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author LXH
 */
public class Reader implements Runnable{
    private static final Logger LOG=Logger.getLogger(Reader.class.getName());
    @Override
    public void run() {
        Thread.currentThread().setName("Thread_Reader");
        LOG.info("Start_Reader");
        String path=Startup.config.getProperty("path");//原混合文件夹位置
        while(!Startup.stop){
            List<String> list=FileTool.getFile(path);//读取原混合文件需要拆分的文件集合
            //如果没有读取到内容，睡觉，重新读
            if(list.size()==0){
                try {
                    LOG.info(path+"is Empty");
                    Thread.sleep(10000l);
                } catch (InterruptedException ex) {
                    LOG.error(ex.getMessage());
                }
                continue;
            }
            
            //放入队列
            for(String str:list){
                try {
                    Startup.queue.put(str);
                    new File(path+File.separator+str+".sns").createNewFile();//标记
                    LOG.info("mixfile "+str);
                } catch (IOException ex){
                    LOG.error(ex.getMessage(),ex);
                } catch (InterruptedException ex) {
                    LOG.error(ex.getMessage(),ex);
                }catch(Exception e){
                    LOG.error(e.getMessage(),e);
                }
            }
        }
        LOG.info("Reader over");
        new Writer().run();//如果队列先消费完，队列为空，消费者可以停止，而生产者卡在获取文件集合和推送数据的过程中，而后又在消费者关闭后推送数据，会导致这部分文件集合未消费
    }
}
